/**
 * SimulatorTokenizer implements Tokenizer and is designed for use with Simulator.<br><br>
 * @author Meghan Day
 * Change Log:<br>
 *  - 05/25/09 - Meghan Day<br>
 *  	- Created skeleton<br><br>
 *  	- Filled in removeSeparators and getNextToken
 */
public class SimulatorTokenizer implements Tokenizer{

	/**
	 * Removes all separator characters from the beginning of sourceLine.
	 * @param sourceLine The line from which to remove separators.
	 */
	private void removeSeparators(StringBuffer sourceLine){
		while ((sourceLine.length() > 0) && ((sourceLine.charAt(0) == ' ') ||
				(sourceLine.charAt(0) == '\t') || (sourceLine.charAt(0) == '\n')
				|| (sourceLine.charAt(0) == '|'))) {
			sourceLine.deleteCharAt(0);
		}
	}
	
	/**
	 * Extracts characters from sourceLine and puts them in token until a separator
	 * is reached. Removes all separator tokens so that sourceLine is returned empty
	 * or beginning with a non-separator character.
	 * @param sourceLine The line from which to extract a token.
	 * @param token The destination for the extracted token.
	 * @param type The type of the token.
	 */
	public void getNextToken(StringBuffer sourceLine, StringBuffer token,
			Assembler.TokenType[] type) {
		if (token.length() > 0) {
			token.delete(0, token.length());
		}
		type[0] = Assembler.TokenType.DATA;

		while ((sourceLine.length() > 0) && !((sourceLine.charAt(0) == ' ') ||
				(sourceLine.charAt(0) == '\t') || (sourceLine.charAt(0) == '\n')
				|| (sourceLine.charAt(0) == '|'))) {
			token.append(sourceLine.charAt(0));
			sourceLine.deleteCharAt(0);
		}
		
		removeSeparators(sourceLine);
	}

}
